layui.use(['form','jquery','table','layer'],function(){
    let form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    //取URL传过来的值
    var loc=location.href;
    var n1=loc.length;//地址的总长度
    var n2=loc.indexOf("=");//取得=号的位置
    var n3=loc.indexOf("&");//取得&号的位置
    var id=decodeURI(loc.substr(n2+1, n3-n2-1));//从?=号后面到&的内容，即id的值

    //加载数据表格

    let recordTable = table.render({
        elem: '#recordList'
        , url: '/contact-record/list/'+id //数据接口
        , page: true,
        limit:5, //每页显示的条数，默认10
        limits:[2,5,10,15,], //每页显示条数选择框数据
        request:{
            pageName: 'current',
            limitName: 'size'
        }
        ,cols: [[ //表头
            {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'},
            {field: 'place', title: '地点'},
            {field: 'contactTime', title: '时间'},
            {field: 'outline', title: '概要'},
            {field: 'remarks', title: '备注'},
            {field: 'detailedInfo', title: '详细信息'},
            {title: '操作',fixed:'right',toolbar:"#recordTool"}
        ]]
    });

    // 新建(交往记录)按钮
    $('.add_rec_btn').click(function () {
        layer.open({
            type:2,
            content:'views/contact-record/add-rec.jsp?id='+id,
            skin:'layui-layer-molv',
            title:"添加交往记录",
            area:["650px","500px"],
        });
    });



    //工具栏：编辑和删除
    table.on('tool(recordList)',function (obj) {
        switch (obj.event){

            case 'edit_btn':
                layer.open({
                    type:2,
                    content:"contact-record/editPage/"+obj.data.id,
                    skin:'layui-layer-molv',
                    title:"编辑交往记录",
                    area:["650px","560px"],
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function () {
                    $.post('contact-record/del/'+obj.data.id,function (data) {
                        recordTable.reload();
                        layer.msg(data.msg);
                    });
                });
        }
    });

    // 编辑后保存按钮
    form.on('submit(btn_submit)',function(){0
        window.parent.location.reload(recordTable); //刷新父页面
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);  // 关闭layer
    });

    $('.back_btn').click(function () {
        javaScript:history.go(-1);

    })
    //关闭按钮
    $('.btn_close').click(function(){
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    });
});